<?php

/**
 * 推广记录
 * File Name：GoodsSpreadModel.php
 * File Encoding：UTF-8
 * File New Time：2014-7-23 14:43:06
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class GoodsSpreadModel extends BaseModel {

    public function tableName() {
        return '{{goods_spread}}';
    }

    public function rules() {
        return array(
            array('userid', 'required', 'message' => '推广用户ID不能为空！'),
            array('username', 'required', 'message' => '推广用户名不能为空！'),
            array('goods_bm_id', 'required', 'message' => '报名ID不能为空！'),
            array('goods_bm_status,ip,create_time,status_time', 'safe'),
        );
    }

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    //关联查询
    public function relations() {
        return array(
            'GoodsBmModel' => array(self::BELONGS_TO, 'GoodsBmModel', 'goods_bm_id'),
        );
    }

    //数据保存前操作
    protected function beforeSave() {
        //是否新增
        if ($this->isNewRecord) {
            $this->ip = GuestDataModel::model()->getForwardedForIp();
            $this->create_time = time();
        }
        return true;
    }

    /**
     * 修改对应状态
     * @param type $goodsBm 报名数据对象
     * @return boolean
     */
    public function saveGoodsStatus($goodsBm) {
        if (!is_object($goodsBm)) {
            return false;
        }
        $data = $this->find($this->where(array('goods_bm_id' => $goodsBm->goods_id)));
        if (empty($data)) {
            return false;
        }
        $data->goods_bm_status = $goodsBm->status;
        $data->status_time = time();
        if ($data->validate() && $data->save()) {
            return true;
        }
        return false;
    }

    /**
     * 根据报名ID删除对应的推广信息
     * @param GoodsBmModel $goods_id 报名ID，或者 报名数据对象
     * @return boolean
     */
    public function deleteSpread($goods_id) {
        if (is_object($goods_id) && $goods_id instanceof GoodsBmModel) {
            $object = $goods_id;
        } else {
            $object = GoodsBmModel::model()->findByPk($goods_id);
        }
        if (empty($object)) {
            $this->addError('', '该报名信息不存在！');
            return false;
        }
        return $this->deleteAll($this->where(array('goods_bm_id' => $object->goods_id)));
    }

    /**
     * 更改推荐人信息
     * @param GoodsBmModel $goods_id 报名ID，或者 报名数据对象
     * @param type $userid 推广会员ID
     * @return boolean
     */
    public function saveSpreadUser($goods_id, $userid) {
        if (empty($userid)) {
            $this->addError('', '推广用户ID不能为空！');
            return false;
        }
        if (is_object($goods_id) && $goods_id instanceof GoodsBmModel) {
            $object = $goods_id;
        } else {
            $object = GoodsBmModel::model()->findByPk($goods_id);
        }
        if (empty($object)) {
            $this->addError('', '该报名信息不存在！');
            return false;
        }
        $data = $this->find($this->where(array('goods_bm_id' => $object->goods_id)));
        if (empty($data)) {
            //增加推广信息
            return $this->addSpreadInfo($object, $userid);
        }
        $userInfo = MembersModel::model()->findByPk($userid);
        if (empty($userInfo)) {
            $this->addError('', '该推广用户不存在！');
            return false;
        }
        //检查是否是推广会员组里的
        $getSpreadMemberList = MembersGroupModel::model()->getSpreadMemberList();
        if (!isset($getSpreadMemberList[$userid])) {
            $this->addError('', '该用户不是推广会员组用户！');
            return false;
        }
        $data->userid = $userInfo->uid;
        $data->username = $userInfo->username;
        if ($data->validate() && $data->save()) {
            return true;
        }
        return false;
    }

    /**
     * 增加推广记录
     * @param GoodsBmModel $goods_id 报名ID，或者 报名数据对象
     * @param type $userid 推广会员ID
     * @return boolean
     */
    public function addSpreadInfo($goods_id, $userid) {
        if (empty($userid)) {
            $this->addError('', '推广用户ID不能为空！');
            return false;
        }
        if (is_object($goods_id) && $goods_id instanceof GoodsBmModel) {
            $object = $goods_id;
        } else {
            $object = GoodsBmModel::model()->findByPk($goods_id);
        }
        if (empty($object)) {
            $this->addError('', '该报名信息不存在！');
            return false;
        }
        $userInfo = MembersModel::model()->findByPk($userid);
        if (empty($userInfo)) {
            $this->addError('', '该推广用户不存在！');
            return false;
        }
        //检查是否是推广会员组里的
        $getSpreadMemberList = MembersGroupModel::model()->getSpreadMemberList();
        if (!isset($getSpreadMemberList[$userid])) {
            $this->addError('', '该用户不是推广会员组用户！');
            return false;
        }
        $this->userid = $userInfo->uid;
        $this->username = $userInfo->username;
        $this->goods_bm_id = $object->goods_id;
        $this->goods_bm_status = $object->status;
        $this->setIsNewRecord(true);
        if ($this->validate() && $this->insert()) {
            return true;
        }
        return false;
    }

}
